<template>
  <div>
    <h1>轮播图列表</h1>
    <div class="nav">
      <div class="serch">
        <div class="input">
          <span>名字:</span>
          <el-input
            v-model="inputWords"
            clearable
            placeholder="请输入"
            @keydown.enter="keyDownFun"
          />
        </div>
        <div class="input">
          <span>分类:</span>
          <el-select
            v-model="inputRegion"
            clearable
            placeholder="请选择"
            placement="bottom-end"
            @change="changeFun"
          >
            <el-option label="全部" value="全部" />
            <el-option
              v-for="item in regionArr"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </div>
        <div class="but">
          <el-button @click=";(inputWords = ''), (inputRegion = '')">
            重置
          </el-button>
          <el-button type="primary" @click="keyDownFun">查询</el-button>
        </div>
      </div>

      <el-dialog v-model="dialogFormVisible" title="轮播图编辑">
        <div>
          <p>
            <span style="color: red">*</span>
            名字
          </p>
          <el-input v-model="newinput" clearable placeholder="请输入分类名字" />
        </div>
        <div>
          <p>
            <span style="color: red">*</span>
            分类:
          </p>
          <el-select
            v-model="newinputRegion"
            clearable
            placeholder="请选择"
            placement="bottom-end"
          >
            <el-option label="全部" value="全部" />
            <el-option
              v-for="item in regionArr"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </div>
        <div>
          <p>简介</p>
          <el-input
            v-model="nwetextarea"
            placeholder="请输入简介"
            :rows="2"
            size="large"
            type="textarea"
          />
        </div>
        <div class="newimg">
          <p>图片</p>

          <label class="addimg">
            <input
              accept="imge/*"
              style="display: none"
              type="file"
              @change="imgFun"
            />

            <vab-icon
              v-show="imgUrl == ''"
              icon="add-fill"
              style="font-size: 80px; color: rgb(197, 197, 197)"
            />

            <img v-show="imgUrl" alt="" :src="imgUrl" style="display: block" />
          </label>
        </div>
        <div>
          <p>链接</p>
          <el-input
            v-model="nwetextlink"
            placeholder="请输入简介"
            :rows="2"
            size="large"
            type="textarea"
          />
        </div>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button v-show="butFlage" type="primary" @click="addBanner">
              确认
            </el-button>
            <el-button
              v-show="butFlage == false"
              type="primary"
              @click="cancelBanner"
            >
              确认
            </el-button>
          </span>
        </template>
      </el-dialog>

      <div class="list">
        <div class="listNav">
          <div class="lef">轮播图分类</div>
          <div class="rig">
            <div>
              <el-button type="primary" @click="dialogFormVisible = true">
                <vab-icon icon="add-line" style="font-size: 20px" />
                新建
              </el-button>
            </div>
            <div @click="refreshFun">
              <el-tooltip
                class="box-item"
                content="刷新"
                effect="dark"
                placement="top"
              >
                <vab-icon icon="restart-line" style="font-size: 20px" />
              </el-tooltip>
            </div>
            <div>
              <el-tooltip
                class="box-item"
                content="密度"
                effect="dark"
                placement="top"
              >
                <vab-icon icon="align-vertically" style="font-size: 20px" />
              </el-tooltip>
            </div>
            <div>
              <el-tooltip
                class="box-item"
                content="列设置"
                effect="dark"
                placement="top"
              >
                <vab-icon icon="settings-3-line" style="font-size: 20px" />
              </el-tooltip>
            </div>
          </div>
        </div>
        <el-table :data="tableData" style="width: 100%; height: 100%">
          <el-table-column
            align="center"
            fixed
            label="序号"
            type="index"
            width="100"
          />
          <el-table-column
            align="center"
            label="名字"
            prop="name"
            width="150"
          />
          <el-table-column
            align="center"
            label="主图"
            width="150"
            v-slot="{ row }"
          >
            <img alt="" :src="'http://localhost:1337' + row.coverImage" />
          </el-table-column>
          <el-table-column
            align="center"
            label="分类"
            prop="category.name"
            width="150"
          />
          <el-table-column
            align="center"
            label="描述"
            prop="desc"
            width="150"
          />
          <el-table-column
            align="center"
            height="150px"
            label="链接"
            style="white-space: nowrap"
            width="150"
            v-slot="{ row }"
          >
            <div
              style="
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
              "
            >
              {{ row.link }}
            </div>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template #default="scope">
              <el-button
                size="small"
                type="primary"
                @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-popconfirm
                cancel-button-text="取消"
                confirm-button-text="确认"
                placement="top"
                title="是否删除"
                @confirm="handleDelete(scope.row)"
              >
                <template #reference>
                  <el-button size="small" type="danger">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div style="padding-top: 20px; padding-bottom: 20px">
      <el-pagination
        v-model:current-page="currentPage"
        background
        layout="prev, pager, next"
        :page-size="10"
        :total="totalNum"
        @current-change="currentChange"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue'
  import {
    getBannerList,
    postFile,
    postManageBannerAdd,
    deleteManageBannerDel,
    getManageBannerLi,
    putManageBannerModify,
  } from '@/myApi/index'

  const dialogFormVisible = ref(false)
  // 定义新建输入框数据
  const nwetextarea = ref('')
  const newinput = ref('')
  const rearImgUrl = ref('')
  const nwetextlink = ref('')
  const newinputRegion = ref('')
  // 上穿图片
  const imgUrl = ref('')
  const imgFun = async (event: any) => {
    const saberimg = new FormData()
    imgUrl.value = URL.createObjectURL(event.target.files[0])
    const file = event.target.files[0]
    saberimg.append('file', file)
    const response = await postFile(saberimg)
    rearImgUrl.value = response.data.data
  }

  // 新增数据
  const butFlage = ref(true)
  const addBanner = async () => {
    const data = await getBannerList(1, 999999999999999)
    console.log(data.data.data)

    const nameID = data.data.data.filter(
      (v: any) => v.name == newinputRegion.value
    )

    console.log(newinputRegion.value)
    butFlage.value = true
    const obj = {
      name: newinput.value,
      desc: nwetextarea.value,
      coverImage: rearImgUrl.value,
      link: nwetextlink.value,
      url: nwetextlink.value,
      category: nameID[0].id,
    }
    if (
      newinput.value &&
      nwetextarea.value &&
      rearImgUrl.value &&
      nwetextlink.value
    ) {
      const data = await postManageBannerAdd(obj)
      console.log(data)

      bannerListFun()
      dialogFormVisible.value = false
    }
  }
  // 清空新增输入框
  watch(
    () => dialogFormVisible.value,
    () => {
      if (dialogFormVisible.value == false) {
        newinput.value = ''
        nwetextarea.value = ''
        imgUrl.value = ''
      }
    }
  )

  // 搜索框
  const inputWords = ref('')
  // 选择搜索
  const inputRegion = ref('')
  const tableData: any = ref([])
  const regionArr: any = ref([])
  const currentPage = ref(1)
  const totalNum = ref(0)
  const bannerListFun = async () => {
    const obj = {
      per: 10,
      page: currentPage.value,
    }
    const data = await getManageBannerLi(obj)
    tableData.value = data.data.data
    console.log(tableData.value)
  }

  const regionArrFun = async () => {
    const data = await getBannerList(1, 999999999999999)
    totalNum.value = data.data.data.length + 1
    data.data.data.map((v: any) => {
      regionArr.value.push(v.name)
    })
    regionArr.value = [...new Set(regionArr.value)].reverse()
  }
  regionArrFun()
  bannerListFun()

  // 模糊查询
  const keyDownFun = () => {
    if (inputWords.value != '') {
      tableData.value = tableData.value.filter(
        (v: any) =>
          v.name.includes(inputWords.value) ||
          v.desc.includes(inputWords.value) ||
          v.category.name.includes(inputWords.value)
      )
      currentPage.value = 1
    }
  }
  watch(
    () => {
      return inputWords.value == ''
    },
    () => {
      currentPage.value = 1
      bannerListFun()
    }
  )
  const changeFun = () => {
    console.log(inputRegion.value)
    console.log(tableData.value)

    if (inputRegion.value != '全部') {
      tableData.value = tableData.value.filter((v: any) =>
        v.category.name.includes(inputRegion.value)
      )
    } else {
      currentPage.value = 1
      bannerListFun()
    }
  }

  // 刷新按钮
  const refreshFun = () => {
    location.reload()
  }

  // 列表删除的按钮
  // 删除按钮
  const handleDelete = async (row: any) => {
    const data = await deleteManageBannerDel(row.id)
    console.log(data)

    bannerListFun()
  }
  // 编辑按钮
  let modifyBannerId = ''
  const handleEdit = (row: any) => {
    // 控制是 新建确定按钮 还是 编辑确定按钮
    butFlage.value = false
    dialogFormVisible.value = true

    nwetextarea.value = row.desc
    newinput.value = row.name
    rearImgUrl.value = row.coverImage
    imgUrl.value = `http://localhost:1337${row.coverImage}`
    nwetextlink.value = row.link
    newinputRegion.value = row.category.name
    modifyBannerId = row.id
  }
  const cancelBanner = async () => {
    const data = await getBannerList(1, 999999999999999)
    console.log(data.data.data)

    const nameID = data.data.data.filter(
      (v: any) => v.name == newinputRegion.value
    )
    console.log(nameID)

    const obj = {
      name: newinput.value,
      desc: nwetextarea.value,
      coverImage: rearImgUrl.value,
      link: nwetextlink.value,
      url: nwetextlink.value,
      category: nameID[0].id,
    }
    if (newinput.value && nwetextarea.value && rearImgUrl.value) {
      await putManageBannerModify(modifyBannerId, obj)
      bannerListFun()
      dialogFormVisible.value = false
    }
  }

  // 分页器
  const currentChange = async () => {
    bannerListFun()
  }
</script>

<style scoped lang="scss">
  h1 {
    height: 50px;
    line-height: 50px;
  }
  .nav {
    width: 100%;
    background-color: #f6f8f9;
    padding: 30px;
    box-sizing: border-box;

    .serch {
      height: 80px;
      width: 100%;
      margin-bottom: 20px;
      background-color: white;
      padding: 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      .input {
        width: 270px;
        height: 80px;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        span {
          display: block;
          width: 50px;
          height: 80px;
          line-height: 80px;
        }
      }
    }

    .list {
      width: 100%;
      height: 100%;
      background-color: white;
      margin: 0 auto;
      text-align: center;
      padding-left: 30px;
      padding-right: 30px;

      .listNav {
        height: 60px;
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .rig {
          display: flex;
          justify-content: space-between;
          align-items: center;
          div {
            margin-left: 10px;
          }
        }
      }
      .rig > div:hover {
        color: #1890ff;
      }
      img {
        width: 80px;
      }
    }
  }
  .newimg {
    .addimg {
      display: block;
      width: 150px;
      height: 150px;
      border: 1px dashed rgb(197, 197, 197);
      border-radius: 10px;
      box-sizing: border-box;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 150px;
      }
    }
  }
  .newimg > .addimg:hover {
    border-color: #1890ff;
    box-sizing: border-box;
  }
</style>
